package com.zhl.test;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;

public class CRUD2 {
	DB db;
/*	CRUD2(){
		String DBName = "test";
		this.db = conn(DBName);
	}*/
	
	public static void main(String[] args) {
		CRUD2 mongo = new CRUD2();
		String DBName = "test";
		mongo.db = mongo.conn(DBName);
		//mongo.findAll("mycolbbb");
		BasicDBObject query = new BasicDBObject();  
		//query = new BasicDBObject();  
		query.put("title", "MongoDB");  //  20 < i <= 30  new BasicDBObject("$gt", 20).append("$lte", 900000)
		//query.put("_id", new ObjectId("58ca2a3101ccf929c8fd4d79")); //where likes = 840023
		mongo.findOne("mycolbbb", query);
		//query.put("title","MongoDB");
		//mongo.findAll("mycolbbb", query);
		/*query.put("title","MongoDB");
		BasicDBObject res = new BasicDBObject();  
		res.put("天王盖地虎", "宝塔镇河妖");
		BasicDBObject value = new BasicDBObject();  
		value.put("$set", res);
		mongo.update("mycolbbb", query, value);*/
		//mongo.findAll("mycolbbb");
		//BasicDBObject query = new BasicDBObject();  
		//query = new BasicDBObject();  
		//query.put("title", "MongoDB");  //  20 < i <= 30  new BasicDBObject("$gt", 20).append("$lte", 900000)
		//query.put("description", "database3"); //where description = database3
		//query.put("_id",new ObjectId("58ca2a3101ccf929c8fd4d79"));//指定id删除
		//mongo.deleteAll("mycolbbb", query);
		//System.out.println("__________________已删除——————————————————————————");
		//mongo.findAll("mycolbbb");
		/*System.out.println("__________________开始新增——————————————————————————");
		
		
		for(int i = 0; i<10000; i++){
			query = new BasicDBObject("title", "MongoDB").
					   append("description", "database3").
					   append("likes", 1003).
					   append("url", "http://www.朱宏亮.com/mongodb3/").
					   append("by", "朱宏亮.com3");
			mongo.insert("mycolbbb", query);
				
		}*/
		
		DBCursor cursor = mongo.findAll("mycolbbb");

		System.out.println("length:"+cursor.length()+"\t count():"+cursor.count()+"\t itcount():"+cursor.itcount());
		
	}


	public void insert(String tableName, BasicDBObject query){
		DBCollection coll = db.getCollection(tableName);

		coll.insert(query);
	
	}
	

	//获取所有文档
	public DBCursor findAll(String tableName){
		//DB db = conn();
		DBCursor cursor;
		DBCollection coll = db.getCollection(tableName);
		cursor = coll.find();   
		System.out.println("数量："+cursor.count());
		/*while(cursor.hasNext()) {  
		   System.out.println(cursor.next());  
		} */
		
		//cursor.close();
		return cursor;
	}
	
	//符合条件的第一个
	public void findOne(String tableName, BasicDBObject query){
		DBObject cursor;
		DBCollection coll = db.getCollection(tableName);
		cursor = coll.findOne(query);  
		System.out.println(cursor.toString());
		for(String a : cursor.keySet()){
			System.out.println(a+"\t"+cursor.get(a));
		}
	}
	
	//获得所有符合条件的文档
	public void findAll(String tableName, BasicDBObject query){
		DBCursor cursor;
		DBCollection coll = db.getCollection(tableName);
		cursor = coll.find(query);  
		//System.out.println(cursor.toString());
		System.out.println("数量："+cursor.count());
		/*while(cursor.hasNext()) {  
		   System.out.println(cursor.next());  
		} */
		cursor.close();
	}
	
	//修改
	public void update(String tableName, BasicDBObject query, BasicDBObject value){
		DBCollection coll = db.getCollection(tableName);
		int num = coll.update(query,value,false,true).getN();
		System.out.println("删除："+num);
	}
	
	//删除一条符合条件的文档
	public void delete(String tableName, BasicDBObject query){
		DBCollection coll = db.getCollection(tableName);
		DBObject myDoc =  coll.findOne(query);
		int num = coll.remove(myDoc).getN();//删除
		System.out.println("删除："+num);
	}
	
	//删除所有符合条件的文档
	public void deleteAll(String tableName, BasicDBObject query){
		DBCollection coll = db.getCollection(tableName);
		int num = coll.remove(query).getN();//删除
		System.out.println("删除："+num);
	}
	
	public DB conn(String DBName){
		 MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
	     DB db = mongoClient.getDB( DBName );
	    
	     return db;
	}
	
	
}
